package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.AdmissionResult;
import com.yunji.news.entity.QualificationList;
import com.yunji.news.mapper.AdmissionResultMapper;
import com.yunji.news.service.AdmissionResultService;
import com.yunji.news.service.QualificationListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 录取结果服务实现类
 *
 * @author yunji
 * @since 2024-11-26
 */
@Service
public class AdmissionResultServiceImpl extends ServiceImpl<AdmissionResultMapper, AdmissionResult> 
        implements AdmissionResultService {

    @Autowired
    private QualificationListService qualificationService;

    @Override
    public IPage<AdmissionResult> getAdmissionPage(Integer page, Integer size, 
                                                  Long schoolId, Long majorId, 
                                                  String admissionStatus, String keyword) {
        Page<AdmissionResult> pageParam = new Page<>(page, size);
        return baseMapper.selectAdmissionPage(pageParam, schoolId, majorId, admissionStatus, keyword);
    }

    @Override
    public boolean generateAdmissionResults(Long schoolId, Long majorId, Integer admissionCount) {
        // 查询入围名单，按排名顺序
        LambdaQueryWrapper<QualificationList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualificationList::getSchoolId, schoolId)
               .eq(QualificationList::getMajorId, majorId)
               .eq(QualificationList::getStatus, 2)
               .orderByAsc(QualificationList::getRankInMajor)
               .last("LIMIT " + admissionCount);

        List<QualificationList> qualifications = qualificationService.list(wrapper);

        // 生成录取结果
        for (int i = 0; i < qualifications.size(); i++) {
            QualificationList qualification = qualifications.get(i);
            AdmissionResult result = new AdmissionResult();
            result.setApplicationId(qualification.getApplicationId());
            result.setSchoolId(schoolId);
            result.setMajorId(majorId);
            result.setStudentName(qualification.getStudentName());
            result.setIdCard(qualification.getIdCard());
            result.setTotalScore(qualification.getTotalScore());
            result.setRankInMajor(i + 1);
            result.setAdmissionStatus("admitted");
            result.setAdmissionType("early_admission");
            result.setConfirmStatus("pending");
            result.setStatus(1);
            
            save(result);
        }

        return true;
    }

    @Override
    public boolean publishAdmissionResults(Long[] ids) {
        LambdaUpdateWrapper<AdmissionResult> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(AdmissionResult::getId, Arrays.asList(ids))
               .set(AdmissionResult::getPublishTime, LocalDateTime.now())
               .set(AdmissionResult::getStatus, 2);
        return update(wrapper);
    }

    @Override
    public List<AdmissionResult> getPublicAdmissionList(Long schoolId, Long majorId, String admissionStatus) {
        LambdaQueryWrapper<AdmissionResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(schoolId != null, AdmissionResult::getSchoolId, schoolId)
               .eq(majorId != null, AdmissionResult::getMajorId, majorId)
               .eq(admissionStatus != null, AdmissionResult::getAdmissionStatus, admissionStatus)
               .eq(AdmissionResult::getStatus, 2)
               .orderByAsc(AdmissionResult::getRankInMajor);
        return list(wrapper);
    }

    @Override
    public AdmissionResult getStudentAdmissionResult(String idCard, Long schoolId) {
        LambdaQueryWrapper<AdmissionResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdmissionResult::getIdCard, idCard)
               .eq(schoolId != null, AdmissionResult::getSchoolId, schoolId)
               .eq(AdmissionResult::getStatus, 2);
        return getOne(wrapper);
    }

    @Override
    public boolean confirmAdmission(Long id) {
        LambdaUpdateWrapper<AdmissionResult> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AdmissionResult::getId, id)
               .set(AdmissionResult::getConfirmStatus, "confirmed")
               .set(AdmissionResult::getConfirmTime, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public boolean batchUpdateAdmissionStatus(Long[] ids, String admissionStatus) {
        LambdaUpdateWrapper<AdmissionResult> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(AdmissionResult::getId, Arrays.asList(ids))
               .set(AdmissionResult::getAdmissionStatus, admissionStatus);
        return update(wrapper);
    }

    @Override
    public boolean sendAdmissionNotice(Long id) {
        LambdaUpdateWrapper<AdmissionResult> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AdmissionResult::getId, id)
               .set(AdmissionResult::getNoticeSent, 1);
        return update(wrapper);
    }
}